﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.DataAccess
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using System.Text;

    using Boiling.Common;

    public class b_Activity : IDisposable
    {
        private SqlDataAdapter dscommand;

        public b_Activity()
        {
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
                return;
            if (dscommand != null)
            {
                if (dscommand.SelectCommand != null)
                {
                    if (dscommand.SelectCommand.Connection != null)
                        dscommand.SelectCommand.Connection.Dispose();
                    dscommand.SelectCommand.Dispose();
                }
                dscommand.Dispose();
                dscommand = null;
            }
        }
        //
        //指令资源分配方式的代码段，与逻辑无关
        //

        public bool insertact(b_Activitydata tempdata)
        {
            //输入一个活动数据，放入数据库，存放成功返回true
            string actfounder = tempdata.Tables["b_Activity"].Rows[0][1].ToString();
            string actname = tempdata.Tables["b_Activity"].Rows[0][2].ToString();
            string actdate = tempdata.Tables["b_Activity"].Rows[0][3].ToString();
            string actpubdate = tempdata.Tables["b_Activity"].Rows[0][4].ToString();
            string actdes = tempdata.Tables["b_Activity"].Rows[0][5].ToString();
            string actamuid = tempdata.Tables["b_Activity"].Rows[0][6].ToString();
            string actmarks = tempdata.Tables["b_Activity"].Rows[0][7].ToString();
            string actmarksno = tempdata.Tables["b_Activity"].Rows[0][8].ToString();
            string insertstring = "INSERT b_Activity (Act_Founder,Act_Name,Act_Date,Act_Pub_Date,Act_Des,Act_Amu_ID,Act_Marks,Act_Marks_NO) VALUES('" + actfounder + "','" + actname + "','" + actdate + "','" + actpubdate + "','" + actdes + "'," + actamuid + "," + actmarks + "," + actmarksno + ")";
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(insertstring, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            return (true);
        }
        public b_Activitydata getactbyNO(int actid)
        {
            //用活动编号返回活动数据
            //如果不存在对应活动则表为空表
            b_Activitydata newdata = new b_Activitydata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_ID=" + actid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Activity");
            tempconn.Close();
            return (newdata);
        }

        public bool judgeactbyNO(int actid)
        {
            //判断某编号是否存在对应活动
            if (getactbyNO(actid).Tables["b_Activity"].Rows.Count == 0)
                return false;
            else
                return true;
        }


        public b_Activitydata getactbyfounder(string username)
        {
            //输入一个用户注册邮箱，返回其发起的活动的列表
            b_Activitydata newdata = new b_Activitydata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_Founder= '" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Activity");
            tempconn.Close();
            return (newdata);
        }

        public bool judgeactbyfounder(string username)
        {
            //判断某用户邮箱代表的用户是否发起了活动，要求输入用户注册邮箱
            //发起过返回true,否则返回true
            if (getactbyfounder(username).Tables["b_Activity"].Rows.Count == 0)
                return false;
            else
                return true;
        }
        public int countactbyfounder(string username)
        {
            //统计一个用户究竟发起了多少活动，要求输入用户注册邮箱
            return (getactbyfounder(username).Tables["b_Activity"].Rows.Count);
        }

        public b_Activitydata getactbyamuid(int amuid)
        {
            //输入一个娱乐地点的编号，输入与其有关联的所有活动
            b_Activitydata newdata = new b_Activitydata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_Amu_ID=" + amuid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Activity");
            tempconn.Close();
            return (newdata);

        }
        public int countactbyamuid(int amuid)
        {
            //输入一个娱乐地点的编号，输出与其相关的所有活动的列表
            return (getactbyamuid(amuid).Tables["b_Activity"].Rows.Count);
        }

        public bool modifyact(b_Activitydata tempdata)
        {
            //输入一个活动数据，修改与其编号相同的活动的内容为输入数据的内容
            //如果存在编号相同的活动并且修改成功，返回true,否则返回false
            int actid = Int32.Parse(tempdata.Tables["b_Activity"].Rows[0]["Act_ID"].ToString());
            b_Page_Mesdata newdata = new b_Page_Mesdata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_ID =" + actid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");
            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = 0; i < tempdata.Tables["b_Activity"].Columns.Count; i++)
                    newdata.Tables["b_Activity"].Rows[0][i] = tempdata.Tables["b_Activity"].Rows[0][i];
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }

        public bool deleteactbyNO(int actid)
        {
            //输入一个活动的编号
            //如果存在对应编号的活动，则删除之，并返回true
            //否则返回false
            b_Activitydata newdata = new b_Activitydata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_ID =" + actid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");

            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_Activity"].Rows[0].Delete();
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }

        public bool deleteactbyamuid(int amuid)
        {
            //输入一个娱乐地点的编号
            //如果存在对应编号的娱乐地点相关的活动，则从数据库中删除对应信息，并返回true
            //否则返回false
            b_Activitydata newdata = new b_Activitydata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_Amu_ID =" + amuid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");

            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = newdata.Tables["b_Activity"].Rows.Count - 1; i >= 0; i--)
                {
                    newdata.Tables["b_Activity"].Rows[i].Delete();
                }
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }
        public bool deleteactbyusername(string username)
        {
            //输入一个用户注册邮箱
            //如果存在该用户发起的活动，则删除之，并返回true
            //否则返回false
            b_Activitydata newdata = new b_Activitydata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_Founder ='" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");

            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = newdata.Tables["b_Activity"].Rows.Count - 1; i >= 0; i--)
                {
                    newdata.Tables["b_Activity"].Rows[i].Delete();
                }
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }

        public bool markact(int actid, int newmark)
        {
            //输入活动编号，输入新打的分
            //如果存在编号对应活动，则评分人数+1，分数重新加权平均，返回true
            //否则返回false

            b_Page_Mesdata newdata = new b_Page_Mesdata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_ID =" + actid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");
            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                int i = Int32.Parse(newdata.Tables["b_Activity"].Rows[0]["Act_Marks"].ToString());
                int j = Int32.Parse(newdata.Tables["b_Activity"].Rows[0]["Act_Marks_NO"].ToString());
                newdata.Tables["b_Activity"].Rows[0]["Act_Marks"] = (i * j + newmark) / (j + 1);
                newdata.Tables["b_Activity"].Rows[0]["Act_Marks_NO"] = j + 1;
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }



    }
}